import datetime

from Database import database as Db


class Controller:
    # 添加一个人
    @staticmethod
    def add_one(ids, name, identity):
        # 如果已经存在这个姓名则拒绝添加, 并抛出异常
        if Db.check_exit(name):
            raise ChenXiException("在Controller中试图添加一个已经存在的人")
        else:
            Db.add_one(ids, name, identity)

    @staticmethod
    def delete_one(name):
        # 如果名字不存在，则拒绝删除，并抛出异常
        if not Db.check_exit(name):
            raise ChenXiException("在Controller中试图删除一个不存在的人")
        Db.delete_one(name)

    # 进行签到
    @staticmethod
    def add_sign(name):
        Db.add_sign(name)

    # 从时间段内获得签到的人员名单
    # 数据类型为datatime.datatime()
    @staticmethod
    def get_names_from_time(first, second):
        return Db.get_names_from_time(first, second)

    @staticmethod
    def get_info_by_id(id_num):
        return Db.get_info_by_id(id_num)

    @staticmethod
    def get_next_id():
        return Db.get_next_id()

    @staticmethod
    def get_id_by_name(name):
        return Db.get_id_by_name(name)

    @staticmethod
    def get_all_user_information():
        return Db.get_all_user_information()


class ChenXiException(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return self.value
